﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;

namespace DAO
{
    public class CTPhieuNhapDAO
    {
        //public List<CTPhieuNhapDTO> LayThongTinCTPhieuNhap()
        //{
        //    string sql = "SELECT * FROM CTPHIEUNHAP ";
        //    OleDbConnection conn = new OleDbConnection();
        //    conn = DataProvider.ConnectDB();
        //    OleDbCommand cmd = new OleDbCommand(sql, conn);
        //    OleDbDataReader reader = cmd.ExecuteReader();
        //    List<CTPhieuNhapDTO> DanhSach = new List<CTPhieuNhapDTO>();
        //    while (reader.Read())
        //    {
        //        CTPhieuNhapDTO ctpnDTO = new CTPhieuNhapDTO();
        //        ctpnDTO.MaCTPN = reader.GetString(0);
        //        ctpnDTO.MaPN = reader.GetString(1);
        //        ctpnDTO.MaSP = reader.GetString(2);
        //        ctpnDTO.SLNhap = reader.GetInt32(3);
        //        ctpnDTO.DonGia = reader.GetInt32(4);
        //        ctpnDTO.ThanhTien = reader.GetInt32(5);
        //        DanhSach.Add(ctpnDTO);
        //    }
        //    conn.Close();
        //    return DanhSach;

        //}
        //public List<CTPhieuNhapDTO> LayThongTinCTPhieuNhap(string maCTPN)
        //{
        //    string sql = "SELECT * FROM CTPHIEUNHAP WHERE MaCTPN= '" + maCTPN + "'";
        //    OleDbConnection conn = new OleDbConnection();
        //    conn = DataProvider.ConnectDB();
        //    OleDbCommand cmd = new OleDbCommand(sql, conn);
        //    OleDbDataReader reader = cmd.ExecuteReader();
        //    List<CTPhieuNhapDTO> DanhSach = new List<CTPhieuNhapDTO>();
        //    while (reader.Read())
        //    {
        //        CTPhieuNhapDTO ctpnDTO = new CTPhieuNhapDTO();
        //        ctpnDTO.MaCTPN = reader.GetString(0);
        //        ctpnDTO.MaPN = reader.GetString(1);
        //        ctpnDTO.MaSP = reader.GetString(2);
        //        ctpnDTO.SLNhap = reader.GetInt32(3);
        //        ctpnDTO.DonGia = reader.GetInt32(4);
        //        ctpnDTO.ThanhTien = reader.GetInt32(5);
        //        DanhSach.Add(ctpnDTO);
        //    }
        //    return DanhSach;
        //}
        public string LayMaCTPNLonNhat() // Lay mã phieu ban lon nhat
        {
            string sql = "SELECT TOP  1, MACTPN FROM CTPHIEUNHAP  ORDER BY MACTPN DESC  ";
            OleDbConnection conn = new OleDbConnection();
            conn = DataProvider.ConnectDB();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            OleDbDataReader reader = cmd.ExecuteReader();
            CTPhieuNhapDTO ctpnDTO = new CTPhieuNhapDTO();
            while (reader.Read())
            {
                ctpnDTO.MaCTPN = reader.GetString(0);
            }
            conn.Close();
            return ctpnDTO.MaCTPN;
        }
        public string NextID(string lastID, string prefixID)
        {
            if (lastID == null)
            {
                return prefixID + "001";  // fixwidth default
            }
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;

        }
        public string TaoCTNHMoi()
        {
            string LastID = LayMaCTPNLonNhat();
            return NextID(LastID, "CTPN");
        }
        public int ThemCTPhieuNhap(CTPhieuNhapDTO ctpnDTO)
        {
            OleDbConnection conn = DataProvider.ConnectDB();
            string sql = "INSERT INTO CTPHIEUNHAP(MaCTPN,MaPD,MaPN,MaSP,SLNhap,DonGia,ThanhTien) ";
            sql = sql + " VALUES(@MaCTPN,@MaPD,@MaPN,@MaSP,@SLNhap,@DonGia,@ThanhTien) ";
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            OleDbParameter para = cmd.Parameters.Add("@MaCTPN", OleDbType.VarChar);
            para.Value = ctpnDTO.MaCTPN;
            para = cmd.Parameters.Add("@MaPD", OleDbType.VarChar);
            para.Value = ctpnDTO.MaPD;
            para = cmd.Parameters.Add("@MaPN", OleDbType.VarChar);
            para.Value = ctpnDTO.MaPN;
            para = cmd.Parameters.Add("@MaSP", OleDbType.VarChar);
            para.Value = ctpnDTO.MaSP;
            para = cmd.Parameters.Add("@SLNhap", OleDbType.Integer);
            para.Value = ctpnDTO.SLNhap;
            para = cmd.Parameters.Add("@DonGia", OleDbType.Double);
            para.Value = ctpnDTO.DonGia;
            para = cmd.Parameters.Add("@ThanhTien", OleDbType.Double);
            para.Value = ctpnDTO.ThanhTien;
            int res = cmd.ExecuteNonQuery();
            conn.Close();
            if (res >= 0)
                return 1; // thành công
            return 0; // không thành công

        }
        public List<CTPhieuNhapDTO> TheoDoiNhapHang(string MaPN)
        {
            string sql = "SELECT MaSP,SLNhap,ThanhTien FROM CTPHIEUNHAP  WHERE MaPN = '"+MaPN+"'";
            OleDbConnection conn = new OleDbConnection();
            conn = DataProvider.ConnectDB();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            OleDbDataReader reader = cmd.ExecuteReader();
            List<CTPhieuNhapDTO> DanhSach = new List<CTPhieuNhapDTO>();

            while (reader.Read())
            {
                if (reader.IsDBNull(0) || reader.IsDBNull(1) || reader.IsDBNull(2))
                    DanhSach = null;
                else
                {
                    CTPhieuNhapDTO ctpnDTO = new CTPhieuNhapDTO();
                    ctpnDTO.MaSP = reader.GetString(0);
                    ctpnDTO.SLNhap = reader.GetInt32(1);
                    ctpnDTO.ThanhTien = reader.GetDouble(2);
                    DanhSach.Add(ctpnDTO);
                }
            }
            conn.Close();
            return DanhSach;
         
        }
       
    }
}
